using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using gudusoft.gsqlparser;
using gudusoft.gsqlparser.Units;

namespace syntaxcheck
{
    class syntaxcheck
    {
        static int Main(string[] args)
        {
            int c = Environment.TickCount;

            if (args.Length == 0)
            {
                Console.WriteLine("Usage:");
                Console.WriteLine("{0} scriptfile [dbvendor]", Environment.GetCommandLineArgs()[0]);
                Console.WriteLine("dbvendor: oracle, sqlserver,db2,mysql or access, default is sql server");
                return 100;
            }

            TDbVendor dbv = TDbVendor.DbVMssql;

            if (args.Length == 2)
            {
                string dbvendor = args[1];
                if (dbvendor == "access")
                {
                    dbv = TDbVendor.DbVAccess;
                }
                else if (dbvendor == "db2")
                {
                    dbv = TDbVendor.DbVDB2;
                }
                else if (dbvendor == "mssql")
                {
                    dbv = TDbVendor.DbVMssql;
                }
                else if (dbvendor == "mysql")
                {
                    dbv = TDbVendor.DbVMysql;
                }
                else if (dbvendor == "oracle")
                {
                    dbv = TDbVendor.DbVOracle;
                }
            }



            TGSqlParser sqlparser = new TGSqlParser(dbv);

           sqlparser.Sqlfilename = args[0];
            // or set sql text
           //sqlparser.SqlText.Text = "select f from t;";

            int iRet = sqlparser.Parse();

            if (iRet != 0)
            {
                Console.WriteLine("Syntax error found in input sql:");
                Console.WriteLine(sqlparser.ErrorMessages);

                #region get more detail error information
                //for (int i = 0; i < sqlparser.ErrorCount; i++)
                //{
                //    Console.WriteLine("Error number: {0}",i);
                //    Console.WriteLine("Error Token: {0}", sqlparser.SyntaxErrors[i].Token );
                //    Console.WriteLine("Error Position: {0},{1}", sqlparser.SyntaxErrors[i].XPosition, sqlparser.SyntaxErrors[i].YPosition);
                //    Console.WriteLine("Hint Message: {0}", sqlparser.SyntaxErrors[i].Hint);
                //}
                #endregion
            }
            else
            {
                Console.WriteLine("Parse OK");
            }

            return iRet;

        }
    }
}
